﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;

using System.Data;
using System.Data.OleDb;

namespace KitBuilder.Views
{
    /// <summary>
    /// Interaction logic for ImportMatches.xaml
    /// </summary>
    public partial class ImportMatches : UserControl
    {
        private string cardFileName;
        private ArrayList DBList;
        private ArrayList importList;
        private Dictionary<String, String> DBListToImportList = new Dictionary<string,string>();
        private Dictionary<String, String> DBItem;
        DataTable importedFile = new DataTable();
        

        public ImportMatches(string cardFileName)
        {
            InitializeComponent();
            this.cardFileName = cardFileName;

            DBList = new ArrayList();
            importList = new ArrayList();
        }

        private void DBColumn_List_Loaded(object sender, RoutedEventArgs e)
        {

            DBList = Stock.GetPropertyList();

            foreach (string sItem in DBList)
            {
                ListBoxItem dbItem = new ListBoxItem();
                dbItem.Content = sItem;
                DBColumn_List.Items.Add(dbItem);

            }

        }


        private void DBColumn_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

        /**
         * Match the selected items.
         */
        private void MatchBtn_Click(object sender, RoutedEventArgs e)
        {
            String selectedDBItem;
            String selectedImportItem;
            try
            {
                
                selectedDBItem = DBColumn_List.SelectedItem.ToString().Trim();
                selectedImportItem = ImportFileColumn_List.SelectedItem.ToString().Trim();
            }
            catch (NullReferenceException nfe)
            {
                MessageBox.Show("Please select items from both boxes");
                return;
            }

            if (selectedDBItem.Length > 0 && selectedImportItem.Length > 0){
                // Both boxes have selected items
                //if DBListToImportList.ContainsKey(selectedDBItem)
                if (DBListToImportList.ContainsKey(selectedDBItem)){
                    DBListToImportList.Remove(selectedDBItem);
                }

                DBListToImportList.Add(selectedDBItem, selectedImportItem);
                

             }

            //MessageBox.Show(DBListToImportList.Count.ToString());

            
        }

        /**
         * Retrieve external file columns
         **/
        private void ImportFileColumn_List_Loaded(object sender, RoutedEventArgs e)
        {
            
            OleDbConnection dbConnection = new OleDbConnection(
                (@"Provider=Microsoft.Jet.OLEDB.4.0;"
                 + @"Data Source=D:\Applications\tables\stock.xls;"
                 + @"Extended Properties=""Excel 8.0;HDR=Yes;"""));

            dbConnection.Open();

            try
            {
                OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", dbConnection);
                dbAdapter.Fill(importedFile);
            }
            finally
            {
                dbConnection.Close();
            }
            
            foreach (DataColumn s in importedFile.Columns)
            {
                ListBoxItem excelItem = new ListBoxItem();
                excelItem.Content = s.ToString();
                ImportFileColumn_List.Items.Add(excelItem);
            }

            foreach (DataRow r in importedFile.Rows)
            {
                ListBoxItem excelItem = new ListBoxItem();
                excelItem.Content = r[1];
                //ImportFileColumn_List.Items.Add(excelItem);
            }
        }

        private void FinalMatchBtn_Click(object sender, RoutedEventArgs e)
        {
            int totalDBColumns = DBList.Count;
            int matchedItems = DBListToImportList.Count;

            
            if (totalDBColumns > matchedItems)
            {
                MessageBox.Show("Total Database columns: " + totalDBColumns.ToString() + "\n" + "Total matched: " + matchedItems.ToString());
            }
        }

        public Dictionary<String, String> GetItemMatches()
        {
            if (DBListToImportList != null)
            {
                return DBListToImportList;
            }
            else
            {
                MessageBox.Show("Empty Match");
                return null;
            }
        }

    }
}
